<?php namespace Home\Controller;
use Common\Model\Address;
use Common\Model\Goods_order;
use Common\Model\Order_list;
use Common\Model\Goods_attr;
use Common\Model\Goods;
//订单控制器
class OrderController extends CommonController{
	public function index(){
		if(isset($_SESSION['nickuser'])){
			$uid=$_SESSION['nickuser']['uid'];
			//获取当前用户的收货地址
			$addressModel=new Address;
			$addressData=$addressModel->where("shop_user_uid={$uid}")->get();
	//		p($addressData);
			View::with('addressData',$addressData);
			//获取订单信息
			$goid=$_SESSION['goid'];
			$goodsOrderModel=new Goods_order;
			$orderData=$goodsOrderModel->where("goid={$goid}")->find();
	//		p($orderData);
			View::with('orderData',$orderData);
			//获取订单列表信息
			$goodsListModel=new Order_list;
			$goodsModel=new Goods;
			$goodsListData=$goodsListModel->where("shop_goodsorder_goid={$goid}")->get();
			foreach($goodsListData as $k=>$v){
				$gid=$v['shop_goods_gid'];
				$goodsData=$goodsModel->where("gid={$gid}")->find();
				$goodsListData[$k]['gname']=$goodsData['gname'];
				$goodsListData[$k]['pic']=$goodsData['pic'];
			}
//			p($goodsListData);
			View::with('goodsListData',$goodsListData);
			View::make();
		}else{
			View::error('请登录后再进行操作');
		}
	}
	//添加订单
	public function addOrder(){
		if(isset($_SESSION['nickuser'])){
			$goodsOrderModel=new Goods_order;
			if(IS_AJAX){
				$goid=$goodsOrderModel->store();
				echo json_encode($goid);
			}
		}
	}
	//购物车->添加订单列表
	public function orderList(){
		if(IS_AJAX){
			$OrderListModel=new Order_list;
			$goid=Q('post.goid');
			$OrderListModel->store($goid);
			unset($_SESSION['cart']);
		}
	}
	//内容页->订单列表
	public function olist(){
		$OrderListModel=new Order_list;
		if(IS_AJAX){
			$OrderListModel->addList();
		}
	}
	//提交订单，到付款页面
	public function getOrder(){
		$goodsOrderModel=new Goods_order;
		if(IS_POST){
			if(!$goodsOrderModel->edit()) View::error($goodsOrderModel->getError());
		}
	}
	//返回购物车，先删除相关订单
	public function cart(){
		$goid=$_SESSION['goid'];
		$goodsOrderModel=new Goods_order;
		$goodsOrderModel->where("goid={$goid}")->delete();
		$OrderListModel=new Order_list;
		$OrderListModel->where("shop_goodsorder_goid={$goid}")->delete();
		go(U('Cart/index'));
	}
	//完成订单
	public function endOrder(){
		if(Q('get.goid')){
			$goid=Q('get.goid');
			$_SESSION['goid']=$goid;
		}else{
			$goid=$_SESSION['goid'];
		}
		$goodsOrderModel=new Goods_order;
		$orderData=$goodsOrderModel->join('user_address','shop_user_address_uaid','=','uaid')->where("goid={$goid}")->find();
		View::with('orderData',$orderData);
		$OrderListModel=new Order_list;
		$goodsOrder=$OrderListModel->join('goods','shop_goods_gid','=','gid')->where("shop_goodsorder_goid={$goid}")->get();
//		p($goodsOrder);
		View::with('goodsOrder',$goodsOrder);
		View::make();
	}
	//支付完成
	public function payOrder(){
		if(isset($_SESSION['goid'])){
			$goodsOrderModel=new Goods_order;
		    if($goodsOrderModel->pay()){
		    	unset($_SESSION['goid']);
		    	View::success("支付成功！",U('Presonal/order'));
		    }
		}else{
			View::error("该订单已支付成功！");
		}
	}
}
?>